<?php
    if (!isset($fieldData['requirement']) || $fieldData['requirement']) {
        if (is_array($fieldData)) {
            if (empty($fieldData['type'])) {
                $fieldData['type'] = 'text';
            }
            $fieldTemplate = 'genericField';
            if (file_exists(ROOT . '/app/View/Elements/genericElements/Form/Fields/' . $fieldData['type'] . 'Field.ctp')) {
                $fieldTemplate = $fieldData['type'] . 'Field';
            }
            if (!isset($fieldData['label'])) {
                $fieldData['label'] = Inflector::humanize($fieldData['field']);
            }

            if (!empty($fieldData['populateAction'])) {
                $fieldData['label'] .= sprintf(
                    ' <span class="fas fa-wrench populateActionTrigger" data-request-script="%s" data-update-target="%s"></span>',
                    base64_encode($fieldData['populateAction']),
                    h($modelForForm . Inflector::camelize($fieldData['field']))
                );
                unset($fieldData['populateAction']);
            }

            if (!empty($fieldData['field']) && !empty($fieldDesc[$fieldData['field']])) {
                $fieldData['label'] .= $this->element(
                    'genericElements/Form/formInfo', array(
                        'field' => $fieldData,
                        'fieldDesc' => $fieldDesc[$fieldData['field']],
                        'modelForForm' => $modelForForm
                    )
                );
            }
            $params = array();
            if (!empty($fieldData['class'])) {
                if (is_array($fieldData['class'])) {
                    $class = implode(' ', $fieldData['class']);
                } else {
                    $class = $fieldData['class'];
                }
                $params['class'] = $class;
            } else {
                $params['class'] = '';
            }
            if (!empty($fieldData['autofocus'])) {
                $params['autofocus'] = 1;
            }
            if (empty($fieldData['type']) || $fieldData['type'] !== 'checkbox' ) {
                $params['class'] .= ' form-control';
            }
            if (!empty($fieldData['description'])) {
                if (!isset($params['class'])) {
                    $params['class'] = '';
                }
                $params['class'] .= ' input-with-description';
            }
            if (!empty($fieldData['picker']) && isset($fieldData['picker']['function'])) {
                $fieldData['picker']['text'] = isset($fieldData['picker']['text']) ? $fieldData['picker']['text'] : __('Picker');
                $params['div'] = 'input text input-append';
                $params['after'] = sprintf('<button type="button" class="btn" onclick="%s.call(this);">%s</button>', $fieldData['picker']['function'], __($fieldData['picker']['text']));
            }
            //$params['class'] = sprintf('form-control %s', $params['class']);
            foreach ($fieldData as $k => $fd) {
                if (!isset($simpleFieldAllowlist) || in_array($k, $simpleFieldAllowlist, true) || strpos($k, 'data-') === 0) {
                    $params[$k] = $fd;
                }
            }
            $temp = $this->element('genericElements/Form/Fields/' . $fieldTemplate, array(
                'fieldData' => $fieldData,
                'params' => $params
            ));
            if (!empty($fieldData['hidden'])) {
                $temp = '<span class="hidden">' . $temp . '</span>';
            }
            echo $temp;
            // $fieldsArrayForPersistence []= $modelForForm . \Cake\Utility\Inflector::camelize($fieldData['field']);
        } else {
            echo $fieldData;
        }
    }